import Util from '@/js_sdk/validator/util.js';
const db = uniCloud.database()
const publicData = {
	items: [{
			value: 1,
			name: '活动公告',
		},
		{
			value: 2,
			name: '温馨提示'
		}
	],
	target: [
		'全部可见', '单独发送'
	],
	towshipData: [{
			"value": "app",
			"text": "手机App"
		},
		{
			"value": "公众号",
			"text": "微信公众号"
		},
	],
	searchVal:'',
	imagestyle: {
		"height": 160, // 边框高度
		"width": 160, // 边框宽度
		"border": { // 如果为 Boolean 值，可以控制边框显示与否
			"color": "#eee", // 边框颜色
			"width": "1px", // 边框宽度
			"style": "solid", // 边框样式
		}
	},
}

const publicFun = {
	// 选择平台公告
	radioChange(e) {
		let index =  e.detail.value
		this.formData.type = this.items[index-1]
	},
	// 选择 swiper
	onTarget(index){
		if (this._uid) {
			return
		}
		this.targetIndex = index
		this.formData.targetSend.platform = this.target[index]
	},
	// swiper 2
	onMultipleData(e){
		// console.log(e.detail)
		this.formData.targetSend.label_arr = e.detail.value?e.detail.value:[]
	},
	// swiper3 获取swiper3数据
	getPersonData(push=false){
		const db = uniCloud.database()
		let usersField =  "avatar,nickname,register_env,mobile,status,_id"
		let addressField = "_id,user_id,title,phone"
		let usersData = db.collection("uni-id-users").field(usersField).getTemp({getOne:false})
		let usersAddress = db.collection("users_address").field(addressField).getTemp({getOne:true})
		db.collection(usersData,usersAddress).where(this.where).orderBy('_id desc')
		.skip(this.skip).limit(10).get({getOne:false}).then( res =>{
			let data = res.result.data
			// console.log(data)
			if (push) {
				this.personData.push(...data)
			}else {
				this.personData = data
			}
		})
		
	},
	// swiper3滚动到底部
	scrollBottom(event){
		this.skip += 10
		this.getPersonData(true)
	},
	// swiper3搜索框
	eventSearch(){
		let nameVal = this.searchVal
		// 合并查询条件
		let  MergeWhere = {
			"register_env.app_name":'易派到家'
		};
		let search = {
			$or: [
				{
					'_id.users_address.phone': {$regex: '.*' + nameVal + '.*',$options: 'i'}
				},
				{
					'nickname': {$regex: '.*' + nameVal + '.*',$options: 'i'}
				},
				{
					'mobile': {$regex: '.*' + nameVal + '.*',$options: 'i'}
				}
			]
		};
		if (nameVal.length === 24) {
			MergeWhere = {
				"register_env.app_name":'易派到家',
				"_id._value":nameVal
			};
			search = {
			};
		}
		// 构建搜索条件
		this.where = Object.assign(MergeWhere, search)
		// console.log(this.where )
		this.getPersonData()
	},
	onBlurEdit(e){
		this.formData.abstract = Util.escapeSpecialCharacters(e.slice(0,80))
	},
}

export {
	publicData,
	publicFun
}